package com.jovienne.web.sys_user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jovienne.utils.ResultUtils;
import com.jovienne.utils.ResultVo;
import com.jovienne.web.sys_role.entity.SelectType;
import com.jovienne.web.sys_user.entity.PageParam;
import com.jovienne.web.sys_user.entity.SysUser;
import com.jovienne.web.sys_user.service.SysUserService;
import com.jovienne.web.sys_user_role.entity.SysUserRole;
import com.jovienne.web.sys_user_role.service.SysUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    //新增员工方法
    @PostMapping
    public ResultVo addUser(@RequestBody SysUser sysUser) {
        System.out.println(sysUser);
        //判断用信息是否存在
        QueryWrapper<SysUser> queryWrapper= new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUsername,sysUser.getUsername());
        SysUser one = sysUserService.getOne(queryWrapper);
        if(one!=null){
            //判断存在则直接返回错误
            return ResultUtils.error("用户名已经存在");
        }
        //判断不存在的，先进行密码md5加密
        if(StringUtils.isNotEmpty(sysUser.getPassword())){
            sysUser.setPassword(DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes()));
        }
        //补全前端未提交字段
        sysUser.setIsAdmin("0");
        sysUser.setCreateTime(new Date());

        //将用户存入数据库
        boolean save = sysUserService.save(sysUser);
        if(save){
            return ResultUtils.success("新增用户成功！！");
        }
        return ResultUtils.error("新增用户失败！！");
    }

    //编辑员工
    @PutMapping
    public ResultVo editUser(@RequestBody SysUser sysUser){
        //判断用户名是否存在
        QueryWrapper<SysUser> query = new QueryWrapper<>();

        query.lambda().eq(SysUser::getUsername,sysUser.getUsername());
        SysUser one = sysUserService.getOne(query);
        //如果id重复则不允许修改
        if(one != null && one.getUserId() != sysUser.getUserId())
        {
            return ResultUtils.error("账户已经被占用");
        }
        //密码加密
        if(StringUtils.isNotEmpty(sysUser.getPassword())) {
            sysUser.setPassword(DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes()));
        }
        sysUser.setUpdateTime(new Date());
        //存入数据库
        boolean save = sysUserService.updateById(sysUser);
        if(save){
            return ResultUtils.success("编辑用户成功!");
        }
        return  ResultUtils.error("编辑用户失败!");
    }

    //删除用户
    @DeleteMapping("/{userId}")
    public ResultVo deleteUser(@PathVariable("userId") Long
                                       userId){
        boolean remove = sysUserService.removeById(userId);
        if(remove){
            return ResultUtils.success("删除用户成功!");
        }
        return ResultUtils.error("删除用户失败!");
    }
    //用户列表
    @GetMapping("/list")
    public ResultVo getList(PageParam param){
        IPage<SysUser> list = sysUserService.list(param);
        //密码不显示
        list.getRecords().stream().forEach(item ->{
            item.setPassword("");
        });
        return ResultUtils.success("查询成功",list);
    }

    //根据用户id查询角色id
    @GetMapping("/role")
    public ResultVo getRole(Long userId){
        QueryWrapper<SysUserRole> query = new QueryWrapper<>();
        query.lambda().eq(SysUserRole::getUserId,userId);
        SysUserRole one = sysUserRoleService.getOne(query);
        return ResultUtils.success("查询成功",one);
    }

    //查询课程教练
    @GetMapping("/getTeacher")
    public ResultVo getTeacher(){
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.lambda().eq(SysUser::getUserType,"2");
        List<SysUser> list = sysUserService.list(query);
        //组装后的select数据
        List<SelectType> selectTypeList = new ArrayList<>();
        if(list.size() >0){
            list.stream().forEach(item ->{
                SelectType selectType = new SelectType();
                selectType.setLabel(item.getNickName());
                selectType.setValue(1L);
                selectTypeList.add(selectType);
            });
        }
        return ResultUtils.success("查询成功",selectTypeList);
    }
}
